package com.jt.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemDescMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.vo.EasyUITable;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;

import com.jt.mapper.ItemMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
	
	@Autowired
	private ItemMapper itemMapper;
	@Autowired
	private ItemDescMapper itemDescMapper;


	@Transactional
	@Override
	public EasyUITable findItemByPage(Integer page, Integer rows) {
//		int startIndex=(page-1)*rows;
//		List<Item> itemList=itemMapper.findItemByPage(startIndex,rows);
//		Long total=Long.valueOf(itemMapper.selectCount(null));
		IPage<Item> iPage=new Page<>(page,rows);
		QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
		queryWrapper.orderByDesc("updated");
		iPage=itemMapper.selectPage(iPage, queryWrapper);
		return new EasyUITable(iPage.getTotal(), iPage.getRecords());
	}

	@Transactional
	@Override
	public SysResult saveItem(Item item,ItemDesc itemDesc) {
		item.setStatus(1);
		int row=itemMapper.insert(item);
		itemDesc.setItemId(item.getId());
		int rowdesc=itemDescMapper.insert(itemDesc);
		if (row!=0&&rowdesc!=0){return SysResult.success(item);}
		return SysResult.fail();
	}

	@Transactional
	@Override
	public void updateItem(Item item,ItemDesc itemDesc) {
		itemMapper.updateById(item);
		itemDesc.setItemId(item.getId());
		itemDescMapper.updateById(itemDesc);
	}

	@Transactional
	@Override
	public void deleteItem(String[] ids) {
		itemMapper.deleteBatchIds(Arrays.asList(ids));
		itemDescMapper.deleteBatchIds(Arrays.asList(ids));
	}

	@Override
	public void updateStatus(Long[] ids, Integer status) {
		Item item=new Item();
		item.setStatus(status);
		UpdateWrapper<Item> updateWrapper=new UpdateWrapper<>();
		updateWrapper.in("id", Arrays.asList(ids));
		itemMapper.update(item, updateWrapper);
	}

	@Override
	public ItemDesc findItemDescById(Long itemId) {
		return itemDescMapper.selectById(itemId);
	}


}
